﻿@using Lib.core;
@using Lib.helper;

@using Bll;
@using Model;
@using Model.Sys;
@using Hiwjcn.Web.Models.Link;
@{
    Layout = null;

    var model = ViewData["model"] as LinkEditViewModel;
    if (model == null) { model = new LinkEditViewModel(); }
    if (model.Link == null) { model.Link = new LinkModel(); }
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!-- header page -->
    @Html.Partial(IncludePath("header"))
    <style type="text/css">
        .input_group {
            margin: 5px 0px;
        }

        .img_preview {
            height: 150px;
            width: auto;
        }
    </style>
</head>

<body>
    @Html.Partial(IncludePath("body_top"))

    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="box sm">
                    <input type="hidden" name="id" value="@(model.Link.LinkID)" />
                    <div class="input_group">
                        <div>链接：</div>
                        <input type="text" class="form-control" name="link" value="@(model.Link.Url)" />
                    </div>
                    <div class="input_group">
                        <div>名称：</div>
                        <input type="text" class="form-control" name="name" value="@(model.Link.Name)" />
                    </div>
                    <div class="input_group">
                        <div>标题：</div>
                        <input type="text" class="form-control" name="title" value="@(model.Link.Title)" />
                    </div>
                    <div class="input_group">
                        <div>图片：</div>
                        <input type="hidden" class="form-control" name="image" value="@(model.Link.Image)" />
                        <div class="well">
                            <form action="/admin/link/fileuploadaction/"
                                  method="post"
                                  enctype="multipart/form-data"
                                  target="up_target">
                                <div class="input-group">
                                    <input type="file" name="file" class="form-control" />
                                    <span class="input-group-btn">
                                        <button class="btn btn-primary">上传</button>
                                    </span>
                                </div>
                            </form>
                        </div>
                        <div class="well" id="img_con"></div>
                    </div>
                    <div style="margin: 10px 0px;">
                        <select name="target" class="form-control" style="width: 200px;" data-value="@(model.Link.Target)">
                            <option value="_blank">新窗口打开</option>
                            <option value="_self">当前窗口打开</option>
                        </select>
                    </div>
                    <div class="input_group">
                        <div>排序：</div>
                        <input class="form-control" type="number" name="order" value="@(model.Link.OrderNum)" />
                    </div>
                    <div style="margin: 18px 0px;">
                        <button class="btn btn-primary" id="save_btn">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <iframe src="" id="up_target" class="up_target" name="up_target"
            style="display:none;"></iframe>

    <script type="text/javascript">
        function set_image(url) {
            $("input[name='image']").val(url);
            $("#img_con").html("<img class='img_preview' src='" + url + "' alt='' />");
        }

        $(function () {
            $('#save_btn').click(function () {
                var id = $("input[name='id']");
                var link = $("input[name='link']");
                var name = $("input[name='name']");
                var title = $("input[name='title']");
                var image = $("input[name='image']");
                var target = $("select[name='target']");
                var order = $("input[name='order']");
                postJson('/admin/link/SaveLinkAction/',
                    {
                        id: id.val(),
                        link: link.val(),
                        name: name.val(),
                        title: title.val(),
                        image: image.val(),
                        target: target.val(),
                        order: order.val(),
                        link_type: '@(model.LinkType)'
                    },
                    function (res) {
                        if (res && res.success) {
                            alert('操作成功');
                            reload_parent();
                        }
                        else {
                            alert(res.msg);
                        }
                    });
            });
        });
    </script>

    @if (ConvertHelper.GetString(model.Link.Image).Trim().Length > 0)
    {
        <script type="text/javascript">
            $(function () {
                set_image('@(model.Link.Image)');
            });
        </script>
    }

    @Html.Partial(IncludePath("body_bottom"))
</body>
</html>
